<?php
/**
 * @file WCParserBehaviorISSET.php
 * @author Alejandro Dario Simi
 * @date $Date: 2012-12-28 01:08:14 +0000 (Fri, 28 Dec 2012) $
 *
 * $Id: WCParserBehaviorISSET.php 47 2012-12-28 01:08:14Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA2/includes/parser/WCParserBehaviorISSET.php $
 */

require_once dirname(__FILE__)."/WCParserBehavior.php";

$WCParserBehaviorMatrix["ISSET"]	= "WCParserBehaviorISSET";

class WCParserBehaviorISSET extends WCParserBehavior {
	public static	$WarningNONAME_VARIABLE = "NONAME_VARIABLE";

	public function check() {
		if($this->_node) {
			if(count($this->_node->params()) < 1) {
				$this->_errorMsg = "ISSET without parameters";
				$this->_ok = false;
			} else {
				$this->_ok = true;
				$this->_errorMsg = "Ok";
			}
		} else {
			$this->_errorMsg = "ISSET node is not set";
			$this->_ok = false;
		}

		return $this->isOk();
	}
	public function get() {
		$out = false;
			
		if($this->isOk()) {
			$params = $this->_node->params();

			$varname = isset($params["var"]) ? $params["var"] : false;
			
			if(!$varname) {
				$this->_parser->setWarning(WCParserBehaviorISSET::$WarningNONAME_VARIABLE, "No variable set", false, false, __CLASS__);
			} else {
				$isTrue = $this->_parser->has($varname);

				if(isset($params["!"])) {
					$isTrue = !$isTrue;
				}

				$out = $isTrue ? $this->_node->parseChildren() : "";
			}
		}

		return $out;
	}

	protected function init() {
		$this->_parser->setWarning(WCParserBehaviorISSET::$WarningNONAME_VARIABLE, false, false, "ISSET invocation but now variable name. Use keyword 'var'");
	}
}

?>